<template>
  <el-form
    :rules="rules"
    label-position="left"
    ref="form"
    :model="form"
    label-width="100px"
  >
    <el-row v-if="addType">
      <el-col :span="24">
        <el-form-item label="上级组织">
          <el-input v-model="form.shangjizuzhi"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item class="verify" label="企业名称" prop="companyName">
          <el-input :disabled="isPreview" v-model="form.companyName"></el-input>
          <button
            v-loading="loading"
            v-if="!isPreview"
            class="verify-b"
            @click.prevent="getIdentityCode"
          >
            验证
          </button>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="企业信用码" prop="identityCode">
          <el-input
            :disabled="isPreview"
            v-model="form.identityCode"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="管理员姓名" prop="manageUserName">
          <el-input
            :disabled="isPreview"
            v-model="form.manageUserName"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="管理员账号" prop="manageUserMobile">
          <el-input
            :disabled="isPreview"
            v-model="form.manageUserMobile"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row style="text-align: center">
      <el-col>
        <el-button
          v-loading="loading1"
          class="dialogBtn"
          type="primary"
          v-if="!isPreview"
          @click="next"
          >下一步</el-button
        >
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { getEnterpriseData } from '@/api/companyRegister'
import { saveCustomerFile1Api } from '@/api/customerFile'
// import { saveCustomerFile1Api } from '@/api/customerFile'
export default {
  name: 'AccountInfo',
  props: ['id', 'addType', 'isPreview', 'companyData'],
  data () {
    return {
      form: {},
      loading: false,
      loading1: false,
      fileData: {},
      rules: {
        companyName: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        identityCode: [
          { required: true, message: '请输入企业信用码', trigger: 'blur' }
        ],
        manageUserName: [
          { required: true, message: '请输入管理员姓名', trigger: 'blur' }
        ],
        manageUserMobile: [
          { required: true, message: '请输入管理员账号', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    if (this.isPreview) {
      this.getInfoData()
    }
  },
  methods: {
    async getInfoData () {
      // const res = await getCustomerInfoApi({ id: this.id })
      // console.log(res)
      const data = this.companyData
      // const data = res.data.entity
      this.form = {
        companyName: data.companyName,
        identityCode: data.enterpriseCode,
        manageUserName: data.manageUserName,
        manageUserMobile: data.manageUserMobile
      }
      // this.checkedList = res.data.entity.businessScope.split(',')
    },
    async getIdentityCode () {
      if (!this.form.companyName) return this.$message.error('请输入公司名称')
      this.loading = true
      try {
        const res = await getEnterpriseData(this.form.companyName)
        console.log(res)
        if (res.data.code == 200) {
          this.$set(this.form, 'identityCode', res.data.data.data.creditNo)
          this.fileData = res.data.data.data
        } else {
          this.$message.error(res.data.msg)
        }
      } catch (error) {
      } finally {
        this.loading = false
      }
      // this.form.identityCode = res.data.data.data.creditNo
    },
    async next () {
      await this.$refs.form.validate()
      this.loading1 = false
      try {
        const res = await saveCustomerFile1Api(this.form)
        console.log(res)
        if (res.code == 200) {
          this.$emit('next', 2, res.data.entity)
          this.$emit('fileData', this.fileData)
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
      } finally {
        this.loading1 = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.verify {
  position: relative;
  .verify-b {
    position: absolute;
    right: 9px;
    bottom: 7px;
    background-color: #409eff;
    color: #fff;
    border: none;
    border-radius: 2px;
    height: 25px;
    width: 50px;
  }
}
</style>
